using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TestImport.Printen;

namespace TestImport.LeerlingRekeningen
{
    public partial class RekeningBeheer : Form
    {
        public RekeningBeheer()
        {
            InitializeComponent();
            this.BackColor = InstParameters.Default.schermkleur;
            onsBoekenPakketDataSet.Leerling.ColumnChanging += new DataColumnChangeEventHandler(Leerling_ColumnChanging);

        }

        void Leerling_ColumnChanging(object sender, DataColumnChangeEventArgs e)
        {
            Text = e.Row["naam"].ToString() + " *** " + e.ProposedValue.ToString();
            if (e.ProposedValue.ToString().StartsWith("a"))
                
                //e.ProposedValue = e.Row["naam"].ToString();
            e.Row.CancelEdit();
        }

        private void rekeningBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.rekeningBindingSource.EndEdit();
            this.rekeningTableAdapter.Update(this.onsBoekenPakketDataSet.Rekening);
        }

        private void RekeningBeheer_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'onsBoekenPakketDataSet.LeerlingBoek' table. You can move, or remove it, as needed.
            this.leerlingBoekTableAdapter.Fill(this.onsBoekenPakketDataSet.LeerlingBoek);
            // TODO: This line of code loads data into the 'onsBoekenPakketDataSet.Leerling' table. You can move, or remove it, as needed.
            this.leerlingTableAdapter.Fill(this.onsBoekenPakketDataSet.Leerling);
            // TODO: This line of code loads data into the 'onsBoekenPakketDataSet.Rekening' table. You can move, or remove it, as needed.
            this.rekeningTableAdapter.Fill(this.onsBoekenPakketDataSet.Rekening);
            for (int i = 0; i < dgvRekeningen.Rows.Count; i++)
                dgvRekeningen.Rows[i].Cells["dgvBerekendBedrag"].Value = i.ToString();

        }

        private void btnPrintRekening_Click(object sender, EventArgs e)
        {
            List<int> lijst = new List<int>();
            DataGridViewSelectedRowCollection sel = dgvRekeningen.SelectedRows;
            if (sel.Count > 0)
            {
                foreach (DataGridViewRow rij in sel)
                {
                    lijst.Add(int.Parse(rij.Cells["dgvRekeningID"].Value.ToString()));
                }
            }
            else
            {
                int vReknr = int.Parse(dgvRekeningen.CurrentRow.Cells["dgvRekeningID"].Value.ToString());
                lijst.Add(vReknr);
            }
            PrintenRekening p = new PrintenRekening(lijst, dgvRekeningen.CurrentRow.Cells["dgvOpmerking"].Value.ToString(), DateTime.Now, 2,true);
            MessageBox.Show(p.printRekeningen());
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            int index= leerlingDataGridView.CurrentRow.Index;
            DataRow drw = onsBoekenPakketDataSet.Tables["Leerling"].Rows[index];
            Text = drw["naam"].ToString();
            string output = "";
            string output2 = "";
            foreach (DataRow dr in onsBoekenPakketDataSet.Tables["Leerling"].Rows)
            {
                foreach (DataColumn dc in onsBoekenPakketDataSet.Tables["Leerling"].Columns)
                {
                    output += dr[dc, DataRowVersion.Current] + "   ";
                    output2 += dr[dc, DataRowVersion.Original] + "   ";
                }
                output += Environment.NewLine;
                output2 += Environment.NewLine;
            }
            MessageBox.Show(output);
            MessageBox.Show(output2);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            int index= leerlingDataGridView.CurrentRow.Index;
            DataRow dr = onsBoekenPakketDataSet.Tables["Leerling"].Rows[index];
            foreach (DataColumn dc in onsBoekenPakketDataSet.Tables["Leerling"].Columns)
            {
                if (!dc.ColumnName.ToString().Equals("leerlingID"))
                    dr[dc] = dr[dc, DataRowVersion.Original];
            }
        }

        private void leerlingDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            bool formateer = false;
            String waarde = "";
            if (this.leerlingDataGridView.Columns[e.ColumnIndex].Name != "dgvLeerlingID")
            //{
                if (e.Value != null)
                {
                    waarde = e.Value.ToString();
                    try
                    {
                        int rij = e.RowIndex;
                        int kolom = e.ColumnIndex;
                        DataRow dr = onsBoekenPakketDataSet.Tables["Leerling"].Rows[rij];
                        if (dr[kolom,DataRowVersion.Original]!=dr[kolom,DataRowVersion.Current])
                        {
                            formateer = true;
                        }
                        if (formateer)
                        {
                            e.FormattingApplied = true;
                            e.CellStyle.BackColor = Color.Pink;
                            e.CellStyle.ForeColor = System.Drawing.Color.DeepPink;
                            e.Value = waarde;
                        }
                    }
                    catch (Exception ex) { }
                }
            //}
        }



        private void leerlingDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //Dummy, moet blijven!!!
        }
    }
}